﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GraphWeight : MonoBehaviour
{
    public NeuralNetwork nn;
    public int fromLayerIndex;
    public int fromNeuralIndex;
    public double weight;
    private LineRenderer lr;
    public float v;

    public Transform startPos;
    public Transform endPos;
    private void Start()
    {
        lr = GetComponent<LineRenderer>();
        lr.material = new Material(Shader.Find("Standard"));
    }
    void LateUpdate()
    {
        if (nn == null) return;
        if (fromNeuralIndex > -1)
        {
            v = (float)(nn.neuralLayers[fromLayerIndex].neurals[fromNeuralIndex].value);
            v = v * (float)weight;
            if (v > 0)
            {
                lr.material.color = Color.Lerp(Color.black, Color.green, v);
            }
            else
            {
                lr.material.color = Color.Lerp(Color.black, Color.red, -v);
            }
        }
        else if (fromNeuralIndex == -1)
        {
            v = (float)weight;
            lr.material.color = Color.Lerp(Color.black, Color.green, v);
        }
        else
        {
            lr.material.color = Color.green;
        }

        if (startPos != null)
        {
            lr.SetPosition(0, startPos.position);
        }
        else
        {
            lr.SetPosition(0, endPos.position - endPos.right * 5);
        }
        if (endPos != null)
        {
            lr.SetPosition(1, endPos.position);
        }
    }
}
